package mest.controller.Modeling;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import mest.entity.SubjectType;
import mest.service.Modeling.SubjectTypeService;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 *@Title SubjectTypeController.java
 *@description TODO
 *@time 2019年12月16日 下午7:17:37
 *@author Twinson Wong
 **/
@RestController
@RequestMapping(value="/subjectType")
@CrossOrigin
public class SubjectTypeController {

	@Resource
	SubjectTypeService subjectTypeService;
	
	/**
	 * 查询列表
	 * @param jsonObject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selectAll")
	public ResultObj selectAll(int index,int pageSize,String subjectName, HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", (index-1)*pageSize);
			map.put("pageSize", pageSize);
			map.put("subjectName", subjectName);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			List<SubjectType> list = subjectTypeService.selectAll(map);
			int count = subjectTypeService.count(map);
			ResultObj resultObj = ResultObj.createSuccess();
			resultObj.setCount(count);
			resultObj.setData(list);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 插入
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/insert")
	public ResultObj insert(@RequestBody JSONObject json, HttpServletRequest request){
//		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			ResultObj resultObj = subjectTypeService.insert(map);
			return resultObj;
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//			return ResultObj.createErrorMsg(ResultObj.ERROR2);
//		}
	}
	
	/**
	 * 修改
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/update")
	public ResultObj update(@RequestBody JSONObject json, HttpServletRequest request){
//		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			ResultObj resultObj = subjectTypeService.update(map);
			return resultObj;
	/*	} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}*/
	}
	
	/**
	 * 删除
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete")
	public ResultObj delete(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			subjectTypeService.delete(map);
			return ResultObj.createSuccessMsg(ResultObj.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
}
